package cn.topevery.garden.dal.util;


import cn.topevery.garden.entity.po.basic.BaseGeometryEntity;
import hw.topevery.framework.GeometryHelper;
import hw.topevery.framework.dto.PointDto;
import hw.topevery.framework.enums.GeometryTypeEnum;
import org.apache.commons.collections.CollectionUtils;
import org.apache.commons.lang3.ObjectUtils;
import org.gavaghan.geodesy.Ellipsoid;
import org.gavaghan.geodesy.GeodeticCalculator;
import org.gavaghan.geodesy.GeodeticCurve;
import org.gavaghan.geodesy.GlobalCoordinates;
import org.locationtech.jts.geom.Point;

import java.util.List;

/**
 * 地图处理工具类
 *
 * @author loner
 */
public class PointUtils {

    /**
     * 填充经纬度坐标属性
     *
     * @param point
     * @param target
     * @return
     */
    public static void fillXy(PointDto point, BaseGeometryEntity target) {
        if (ObjectUtils.isNotEmpty(point)
                && ObjectUtils.isNotEmpty(point.getLat()) && ObjectUtils.isNotEmpty(point.getLng())) {
            target.geoX = point.getLng();
            target.geoY = point.getLat();
            target.mectorX = GeometryHelper.xyToMercator(point.getLng(), point.getLat()).x;
            target.mectorY = GeometryHelper.xyToMercator(point.getLng(), point.getLat()).y;
        } else {
            target.geoX = null;
            target.geoY = null;
            target.mectorX = null;
            target.mectorY = null;
        }
    }

    /**
     * 填充经纬度坐标属性
     *
     * @param points
     * @param target
     * @return
     */
    public static void fillXy(List<List<PointDto>> points, BaseGeometryEntity target) {
        if (CollectionUtils.isNotEmpty(points)) {
            fillXy(points.get(0).get(0), target);
        }
    }

    /**
     * 填充几何坐标属性
     *
     * @param pointList
     * @param target
     * @param geometryTypeEnum
     */
    public static void fillGeometry(List<List<PointDto>> pointList, BaseGeometryEntity target, GeometryTypeEnum geometryTypeEnum) {
        if (CollectionUtils.isNotEmpty(pointList)) {
            target.geomCol = GeometryHelper.getGeometry(GeometryHelper.ListtoWellKnownText(pointList, geometryTypeEnum));
            target.geoMercator = GeometryHelper.getGeometry(GeometryHelper.MectorListtoWellKnownText(pointList, geometryTypeEnum));

            Point point = target.geomCol.getCentroid();
            Point mercatorPoint = target.geoMercator.getCentroid();

            target.geoX = point.getX();
            target.geoY = point.getY();
            target.mectorX = mercatorPoint.getX();
            target.mectorY = mercatorPoint.getY();
        }
    }

    /**
     * 计算百度坐标距离
     *
     * @param fromLng from 经度
     * @param fromLat from 纬度
     * @param toLng to 经度
     * @param toLat to 纬度
     * @return 距离
     */
    public static double countDistance(double fromLat, double fromLng, double toLat, double toLng) {
        GlobalCoordinates from = new GlobalCoordinates(fromLat,fromLng);
        GlobalCoordinates to = new GlobalCoordinates(toLat,toLng);
        GeodeticCurve geoCurve = new GeodeticCalculator().calculateGeodeticCurve(Ellipsoid.Sphere, from, to);
        return geoCurve.getEllipsoidalDistance();
    }
}
